const { request } = require('./services/request')
// app.js
App({
  onLaunch() {
    // 如果登陆过 则每次启动刷新token
    if (wx.getStorageSync('token')) {
      // 登录
      wx.login({
        success: (res) => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          request('post', '/authorization/wx/registerOrLogin', {
            jsCode: res.code,
            nickName: "测试"
          }).then(logginRes => {
            const {data} = logginRes;
            const {token, userInfo} = data;
            console.log(userInfo)
            wx.setStorageSync('token', token)
            try {
              wx.setStorageSync('userInfo', JSON.stringify(userInfo))
            } catch (error) {
              console.log('error', error)
            }
          }).catch(err => {
          })
        }
      })
    }
    //
    let that = this
    console.log(this)
    wx.getSystemInfo({
      success: (res) => {
        console.log('system :>> ', res)
        that.globalData.logicPixelRatio = 750 / res.safeArea.width
        that.globalData.safeHeight = res.safeArea.height
        that.globalData.footerHeight = res.windowHeight - res.safeArea.bottom
      },
    })
  },
  globalData: {
    userInfo: null,
    safeHeight: 0,
    logicPixelRatio: 0,
    footerHeight: 0,
  },
})
